<?php
namespace Modules\Blog\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Redis;
use Modules\Admin\Models\Picture as PictureModel;
use Modules\BlogAdmin\Models\BlogArticle as BlogArticleModel;
use Modules\BlogAdmin\Models\BlogColumn as BlogColumnModel;
use Modules\BlogAdmin\Models\BlogLabel as BlogLabelModel; 
use Modules\BlogAdmin\Models\BlogArticleLabel as BlogArticleLabelModel; 
use Modules\BlogAdmin\Models\BlogComment as BlogCommentModel; 
use Modules\Blog\Http\Controllers\CommonController;
class IndexController extends CommonController
{
	public function mail(){
		//$a = Redis::set('name', 'Taylor');
		//$a = Redis::get('name');
		//return $a;
		/*Mail::raw('邮件内容',function($message){
			$message->from('phpsongbo@163.com','宋博');
			$message->subject('邮件标题');
			$message->to('997786358@qq.com');
		});
		*/
		/*Mail::send('blog::layouts.mail',['name'=>'word'],function($message){
			$message ->to('997786358@qq.com')->subject('测试邮件');
		});*/
	}
	
	
	
	/**
	 * @name  首页 
	 */
    public function index(Request $request)
    {	
					
		//文章列表
		$id = $request->get('id');
		if(empty($id)){
			$id = 1;
		}
		if($id == 1){
			//轮播图
			$banner = PictureModel::with('image_one')
				->orderBy('sort','asc')->orderBy('id','desc') 
				->where([['status','=','1'],['type','=','1']])
				->get()->toArray();
		}else{
			$banner = [];
		}
		$name = BlogColumnModel::where('id',$id)->value('title');
		//文章列表
		$articles = BlogArticleModel::with('image_one')
				->orderBy('sort','asc')
				->orderBy('id','desc') 
				->where([['blog_column_id','=',$id],['status','=','1']])	
				->paginate(config('blog.common_limit'));
		//点击排行		
		$articles_pv = $this->articles_pv();
		//站长推荐		
		$articles_open = $this->articles_open();			
        return view('blog::index.index',compact('banner','articles','articles_pv','articles_open','id','name'));
    }
	/**
	 * @name  标签 
	 */
    public function label(Request $request)
    {				
		//文章列表
		$id = $request->get('id');
		$name = BlogLabelModel::where('id',$id)->value('name');
		$blog_article_id = BlogArticleLabelModel::where('blog_label_id',$id)->select('blog_article_id')->get()->toArray();
		$blog_article_id = array_column($blog_article_id,'blog_article_id');
		$articles = BlogArticleModel::with('image_one')
				->orderBy('sort','asc')
				->orderBy('id','desc') 
				->where('status','1')
				->whereIn('id',$blog_article_id)	
				->paginate(config('blog.common_limit'));
		//点击排行		
		$articles_pv = $this->articles_pv();
		//站长推荐		
		$articles_open = $this->articles_open();
		$ids = $id;	
        return view('blog::index.label',compact('ids','articles','articles_pv','articles_open','name'));
    }
	/**
	 * @name  搜索 
	 */
    public function search(Request $request)
    {				
		//文章列表
		$title = $request->get('title');
		$articles = BlogArticleModel::with('image_one')
				->orderBy('sort','asc')
				->orderBy('id','desc') 
				->where([['status','=','1'],['title','like','%' . $title . '%']])
				->paginate(config('blog.common_limit'));
		//点击排行		
		$articles_pv = $this->articles_pv();
		//站长推荐		
		$articles_open = $this->articles_open();
        return view('blog::index.search',compact('articles','articles_pv','articles_open','title'));
    }
	/**
	 * @name  地图 
	 */
    public function map()
    {	
        return view('blog::index.map');
    }
	/**
	 * @name  文章详情 
	 */
    public function info(Request $request)
    {	
		$id = $request->get('id');
		$article_info = BlogArticleModel::with(array('comment_many'=>function($quset){
			$quset->orderBy('id','desc');
		}))
							->where([['status','=','1'],['id','=',$id]])
							->first()->toArray();   
		if($article_info){
			BlogArticleModel::where([['status','=','1'],['id','=',$id]])->increment('pv');
		}
		$article_info_s = BlogArticleModel::orderBy('sort','asc')
				->orderBy('id','desc')->where([['status','=','1'],['id','>',$id]])->first();
		$article_info_x = BlogArticleModel::orderBy('sort','asc')
				->orderBy('id','desc')->where([['status','=','1'],['id','<',$id]])->first();
		//点击排行		
		$articles_pv = $this->articles_pv();
		//站长推荐		
		$articles_open = $this->articles_open();	
        return view('blog::index.info',compact('articles_pv','articles_open','article_info','article_info_s','article_info_x'));
    }
	/**
	 * @name  文章评论
	 */
	public function comment(Request $request){
		$data = $request->all();
		$data['created_at'] = date('Y-m-d H:i:s');
		$data['content'] = strip_tags($data['content']);
		if(BlogCommentModel::insert($data)){
			$info = BlogCommentModel::where(['blog_article_id'=>$data['blog_article_id']])->orderBy('id','desc')->get()->toArray();
			return response()->json([
				'status'=>1,
				'info' =>$info 
			],200);
		}else{
			return response()->json([
				'status'=>0,
				'error' => '评论失败！',
				'msg' => ''
			],201);
		};
	}
	/**
	 * @name  关于我 
	 */
    public function my()
    {	
		$id = -1;
        return view('blog::index.my',compact('id'));
    }
	
	
	
}
